﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FlashCards.Model;

namespace FlashCards.WebApp.Models
{
    public class CategoryModel : IComparable
    {
        public CategoryModel(Category category)
        {
            if (category.CategoryId != 0)
                Id = category.CategoryId;
            Name = category.Name;
            ParentId = category.ParentCategoryId;

            if (category.ChildrenCategories.IsLoaded)
            {
                Children = new List<CategoryModel>();
                foreach (var item in category.ChildrenCategories)
                {
                    Children.Add(new CategoryModel(item));
                }

                Children.Sort();
            }
        }

        public int? Id { get; set; }
        public string Name { get; set; }
        public int? ParentId { get; set; }

        public List<CategoryModel> Children { get; set; }

        public int CompareTo(object obj)
        {
            if (obj is CategoryModel)
            {
                var cat = obj as CategoryModel;

                return Name.CompareTo(cat.Name);
            }
            return 0;
        }
    }
}